package com.zhouyu.advice;

import com.zhouyu.service.UserService;
import org.aopalliance.aop.Advice;
import org.jetbrains.annotations.NotNull;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;

import java.lang.reflect.Method;

/**
 * @author yangjian
 */
//@Component
public class TestPointCutAdvisor implements PointcutAdvisor {

	@NotNull
	@Override
	public Advice getAdvice()
	{
		return new TestMethodInterceptor();
	}

	@Override
	public boolean isPerInstance()
	{
		return false;
	}

	@NotNull
	@Override
	public Pointcut getPointcut()
	{
		return new Pointcut() {
			@NotNull
			@Override
			public ClassFilter getClassFilter()
			{
				return new ClassFilter() {
					@Override
					public boolean matches(@NotNull Class<?> clazz)
					{
						// 切面匹配类筛选逻辑
						return clazz.getName().equals(UserService.class.getName());
					}
				};
			}

			@NotNull
			@Override
			public MethodMatcher getMethodMatcher()
			{
				return new MethodMatcher() {
					@Override
					public boolean matches(@NotNull Method method, @NotNull Class<?> targetClass)
					{
						// 这里实现切面方法筛选逻辑
						// return method.getName().equals("test");
						return method.getName().startsWith("test");
					}

					@Override
					public boolean isRuntime()
					{
						return false;
					}

					@Override
					public boolean matches(@NotNull Method method, @NotNull Class<?> targetClass, @NotNull Object... args)
					{
						return false;
					}
				};
			}
		};
	}
}
